package com.akka.akkaacotr;

import akka.actor.UntypedActor;
import com.akka.akkaacotr.model.Message;
import com.akka.akkaacotr.servcie.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

@Component("workerActor")
@Scope("prototype")
public class WorkerActor extends UntypedActor {

    @Autowired
    private BusinessService businessService;

    private int count = 0;

    final private CompletableFuture<Message> future;

    public WorkerActor(CompletableFuture<Message> future) {
        this.future = future;
    }

    @Override
    public void onReceive(Object message) throws Exception {
        if (message instanceof Request) {
            businessService.perform(this + " " + (++count));
        } else if (message instanceof Response) {

            getSender().tell(count, getSelf());
        } else if (message instanceof Message) {
            System.out.println("Thread:"+Thread.currentThread().getName());
            Thread.sleep(1000 * 4);
            future.complete((Message) message);
        } else {

            unhandled(message);
        }
    }

    public static class Request {
    }

    public static class Response {
    }
}